package net.jcip.examples.vehicles.point;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author nulls.narik
 */
public class SafePoint {

    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private double x;
    private double y;

    public SafePoint(double x, double y) {
        set(x, y);
    }

    public SafePoint(double[] args) {
        if (args == null || args.length != 2) {
            throw new IllegalArgumentException();
        }
        set(args[0], args[1]);
    }

    public SafePoint(Point point) {
        this(point.getX(), point.getY());
    }

    public double[] get() {
        double[] result;
        readWriteLock.readLock().lock();
        result = new double[] { x, y };
        readWriteLock.readLock().unlock();
        return result;
    }

    public void set(double x, double y) {
        readWriteLock.writeLock().lock();
        this.x = x;
        this.y = y;
        readWriteLock.writeLock().unlock();
    }

}
